 
package com.wtzz.police.base.util;

import com.wtzz.police.base.model.enums.WtzzResponse;
import com.wtzz.police.base.model.exception.WtzzRuntimeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**  
* 类说明   
*  
* @author zxh  
* @date 2019-05-07   
*/
public class MyCustomDateEditor extends PropertyEditorSupport {

	@Override
	public void setAsText(@Nullable String text) throws IllegalArgumentException {
		if(StringUtils.isEmpty(text)) {
			setValue(null);
			return;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			setValue(sdf.parse(text));
		} catch (ParseException e) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				setValue(sdf.parse(text));
			} catch (ParseException e1) {
				throw new WtzzRuntimeException(WtzzResponse.ERROR_OPTION,"日期转换失败！");
			}
		}
	}
}
